﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ESOL.Controls;
using ESOL.Common;
using ESOL.Business.Sec;

namespace ESOL.WinUI.BaseForm
{
    public partial class frmBaseMdiChildForm : frmBaseForm
    {
        protected bool mCanQueryOtherPerson = false;

        public frmBaseMdiChildForm()
        {
            InitializeComponent();
        }

        private void GetAllButtons(List<DevButton> list, Control control)
        {
            if (list == null)
            {
                list = new List<DevButton>();
            }
            if (control == null || control.Controls == null || control.Controls.Count == 0)
            {
                return;
            }
            foreach (Control con in control.Controls)
            {
                if (con is DevPanel)
                {
                    GetAllButtons(list, con);
                }
                else if (con is DevButton)
                {
                    list.Add((DevButton)con);
                }
            }
        }

        protected virtual void CheckButtonRight()
        {
            string menuID = CommonFunctions.ToString(this.Tag);
            if (string.IsNullOrEmpty(menuID))
            {
                ESOL.Common.Entity.SecMenuEntity menu = ESOL.Business.Sec.SecMenuBs.GetMenuByFormName(this.GetType().FullName);
                if (menu != null)
                    menuID = menu.MenuId;
            }
            List<DevButton> list = new List<DevButton>();
            GetAllButtons(list, this);
            foreach (DevButton button in list)
            {
                if (button.Tag != null && button.Name != "btnQuery")
                {
                    bool hasRight = SecObjectRoleBs.CheckUserObjectRight(CommonParameter.CurrentUser.PersonId, menuID, button.Name);
                    if (!hasRight)
                    {
                        button.Tag = "Disable";
                    }
                    button.Enabled = hasRight;
                }
            }
        }

        private void frmBaseMdiChildForm_Load(object sender, EventArgs e)
        {
            this.Width = Screen.PrimaryScreen.Bounds.Width - 6 - CommonParameter.OffsetX;
            this.Height = Screen.PrimaryScreen.Bounds.Height - 162 - CommonParameter.OffsetY + 25;
            this.StartPosition = FormStartPosition.Manual;
            this.PointToClient(new Point(0, 0));
            if (CommonParameter.CurrentUser != null)
            {
                if (CommonParameter.CurrentUser.PersonId != "1")
                {
                    CheckButtonRight();
                }
            }
        }

        private void frmBaseMdiChildForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //if (this.MdiParent != null)
            //{
            //    EsolToolStrip tsBottom = (EsolToolStrip)this.MdiParent.Controls["tsBottom"];
            //    if (tsBottom != null)
            //    {
            //        for (int i = 0; i < tsBottom.Items.Count; i++)
            //        {
            //            if (tsBottom.Items[i].Text == this.Text)
            //            {
            //                tsBottom.Items.Remove(tsBottom.Items[i]);
            //                if (i != 0 && i % 2 == 0)
            //                {
            //                    tsBottom.Items.Remove(tsBottom.Items[i - 1]);
            //                }
            //                else if (i == 0 && tsBottom.Items.Count > 0)
            //                {
            //                    tsBottom.Items.Remove(tsBottom.Items[i]);
            //                }
            //            }
            //        }
            //    }
            //}
        }
    }
}
